package git.soulbgm.common.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import git.soulbgm.common.enums.CodeEnum;

/**
 * 前端返回格式
 *
 * @author SoulBGM
 * @date 2020-11-18 10:03:05
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"data", "msg", "code"})
public class Response {

    private Integer code;

    private String msg;

    private Object data;

    private Response() {
    }

    /**
     * 成功返回
     *
     * @return 响应对象
     */
    public static Response setOk() {
        return setOk(CodeEnum.SUCCESS, null);
    }

    /**
     * 成功返回
     *
     * @param codeEnum 状态码
     * @return 响应对象
     */
    public static Response setOk(CodeEnum codeEnum) {
        return setOk(codeEnum, null);
    }

    /**
     * 成功返回
     *
     * @param data 数据
     * @return 响应对象
     */
    public static Response setOk(Object data) {
        return Response.setOk(CodeEnum.SUCCESS, data);
    }

    /**
     * 成功返回
     *
     * @param codeEnum 状态码枚举
     * @param data     数据
     * @return 响应对象
     */
    public static Response setOk(CodeEnum codeEnum, Object data) {
        return setOk(codeEnum.getCode(), codeEnum.getMsg(), data);
    }

    /**
     * 成功返回
     *
     * @param code 状态码
     * @param msg  提示信息
     * @return 响应对象
     */
    public static Response setOk(int code, String msg) {
        return setOk(code, msg, null);
    }

    /**
     * 成功返回
     *
     * @param code 状态码
     * @param msg  提示信息
     * @param data 数据
     * @return 响应对象
     */
    public static Response setOk(int code, String msg, Object data) {
        Response response = new Response();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }


    /**
     * 失败返回
     *
     * @return 响应对象
     */
    public static Response setError() {
        return setError(CodeEnum.FAIL);
    }

    /**
     * 失败返回
     *
     * @param codeEnum 状态码
     */
    public static Response setError(CodeEnum codeEnum) {
        Response response = new Response();
        response.setCode(codeEnum.getCode());
        response.setMsg(codeEnum.getMsg());
        return response;
    }

    /**
     * 错误返回
     *
     * @param msg 提示信息
     * @return 响应对象
     */
    public static Response setError(String msg) {
        Response response = new Response();
        response.setCode(CodeEnum.ERROR.getCode());
        response.setMsg(msg);
        return response;
    }

    /**
     * 错误返回
     *
     * @param msg  提示信息
     * @param data 数据
     * @return 响应对象
     */
    public static Response setError(String msg, Object data) {
        Response response = new Response();
        response.setCode(CodeEnum.ERROR.getCode());
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    /**
     * 失败返回
     *
     * @param code 状态码
     * @param msg  提示信息
     * @return 响应对象
     */
    public static Response setError(int code, String msg) {
        Response response = new Response();
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
